package com.example.controller;

import com.example.entity.OrderInfo;
import com.example.mapper.OrderMapper;
import com.example.utils.StringRedisTemplateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author mingtian
 * @version V1.0
 * @Description 订单相关接口
 * @date 2019/11/19 22:39
 */
@RestController
public class OrderController {
    /**
     * 订单前缀
     */
    public static final String ORDER_PREFIX = "order_";

    @Autowired
    private StringRedisTemplateUtil redisTemplateUtil;

    @Autowired
    private OrderMapper orderMapper;

    @GetMapping("/insertOrder")
    public String insertOrder() {
        //1.生成 orderToken key
        String orderToken = ORDER_PREFIX + UUID.randomUUID().toString();
        //2.生成 orderId
        String orderId = ORDER_PREFIX + System.currentTimeMillis();
        //3.将 订单信息存到 redis 中，key=orderToken,value=orderId
        redisTemplateUtil.add(orderToken, orderId, 15L);
        //4.商品库存 -1

        OrderInfo orderInfo1 = new OrderInfo(null, orderId, orderToken, 0, new Date(), "admin");
        //5.插入数据库
        return orderMapper.insert(orderInfo1) > 0 ? "success" : "fail";
    }

    @GetMapping("/getOrder")
    public OrderInfo getOrder(String orderId) {
        OrderInfo orderInfo = orderMapper.getOrderByOrderId(orderId);
        return orderInfo;
    }

    @GetMapping("/getOrderList")
    public List<OrderInfo> getOrderList() {
        List<OrderInfo> orderInfoList = orderMapper.listOrder();
        return orderInfoList;
    }

}
